package cn.hbwe.service.impl;

import cn.hbwe.mapper.DoctorMapper;
import cn.hbwe.mapper.PatientMapper;
import cn.hbwe.pojo.dto.ChartDTO;
import cn.hbwe.pojo.dto.ChartDTOy;
import cn.hbwe.pojo.entity.*;
import cn.hbwe.mapper.ChartMapper;
import cn.hbwe.pojo.vo.ChartVo;
import cn.hbwe.service.IBookingService;
import cn.hbwe.service.IChartService;
import cn.hbwe.service.IDepartmentService;
import cn.hbwe.util.CustomException;
import cn.hbwe.util.PageVO;
import cn.hbwe.util.Result;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 7组
 * @since 2023-10-18
 */
@Service
public class ChartServiceImpl extends ServiceImpl<ChartMapper, Chart> implements IChartService {
    @Autowired
    private ChartMapper chartMapper;

    @Autowired
    private IBookingService bookingService;

    @Autowired
    private DoctorMapper doctorMapper;

    @Autowired
    private IDepartmentService departmentService;


    @Autowired
    private PatientMapper patientMapper;

    public Result insertChart(ChartDTO chartDTO){
        //应先判断病例是否已经添加成功，避免重复添加。但此功能并未完成！
        QueryWrapper<Doctor> qwdoctor=new QueryWrapper<>();
        QueryWrapper<Patient> qwpatient=new QueryWrapper<>();
        Chart chart=new Chart();
        chart.setChartRecord(chartDTO.getChartRecord());
        chart.setDepartmentId(chartDTO.getDepartmentId());
        chart.setDoctorId(chartDTO.getDoctorId());
        chart.setPatientId(chartDTO.getPatientId());
        //判断是否有该医生
        qwdoctor.eq("doctor_id",chartDTO.getDoctorId());
        Doctor doctor=doctorMapper.selectOne(qwdoctor);
        if(doctor==null){
            try {
                throw new CustomException("没有该医生");
            } catch (CustomException e) {
                return Result.error(e.getMessage());
            }
        }
        //是否有该病人
        qwpatient.eq("patient_id",chartDTO.getPatientId());
        Patient patient=patientMapper.selectOne(qwpatient);
        if(patient==null){
            try {
                throw new CustomException("没有该病人的信息");
            } catch (CustomException e) {
                return Result.error(e.getMessage());
            }
        }
        //添加病例
        chartMapper.insert(chart);
        return Result.success("病例添加成功");
    }
    //删除病人简历
    //更改病人简历
    //查看病人简历
    public IPage<Chart> selectChart(Long p){
        QueryWrapper<Chart> queryWrapper = new QueryWrapper<>();
        Page<Chart> page = new Page<>(p, 5);
        IPage<Chart> iPage =chartMapper.selectPage(page,queryWrapper);
        return iPage;
    }



    @Override
    public PageVO<ChartVo> getPageById(Long currentPage, Long pageSize, Integer id) {
        Page<Chart> page = Page.of(currentPage, pageSize);
        LambdaQueryWrapper<Chart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Chart::getPatientId, id);
        Page<Chart> chartPage = page(page, lqw);

        long total = chartPage.getTotal();
        long current = chartPage.getCurrent();
        List<Chart> charts = chartPage.getRecords();
        if (charts == null) {
            return PageVO.of(total, current, Collections.emptyList());
        }

        List<ChartVo> chartVos = BeanUtil.copyToList(charts, ChartVo.class);
        for (Chart chart : charts) {
            Integer bookingId = chart.getBookingId();
            Booking booking = bookingService.getById(bookingId);
            for (ChartVo chartVo : chartVos) {
                if (bookingId == chartVo.getBookingId()) {
                    chartVo.setBookingTime(booking.getBookingTime());
                    chartVo.setTime(booking.getTime());
                }
            }
        }
        return PageVO.of(total,current,chartVos);
    }




    @Override
    public PageVO<ChartVo> getPage(Long currentPage, Long pageSize,String name) {
        Page<Chart> page = Page.of(currentPage, pageSize);
        LambdaQueryWrapper<Chart> lqw = new LambdaQueryWrapper<>();
        lqw.like(name != null, Chart::getPatientName, name);
        Page<Chart> chartPage = page(page, lqw);

        long total = chartPage.getTotal();
        long current = chartPage.getCurrent();
        List<Chart> charts = chartPage.getRecords();
        if (charts == null) {
            return PageVO.of(total, current, Collections.emptyList());
        }

        List<ChartVo> chartVos = BeanUtil.copyToList(charts, ChartVo.class);
        for (Chart chart : charts) {
            Integer departmentId=chart.getDepartmentId();
            Department department = departmentService.getById(departmentId);

            Integer bookingId = chart.getBookingId();
            Booking booking = bookingService.getById(bookingId);

            for (ChartVo chartVo : chartVos) {
                if (bookingId == chartVo.getBookingId()) {
                    chartVo.setBookingTime(booking.getBookingTime());
                    chartVo.setTime(booking.getTime());
                }
                if (departmentId == chartVo.getDepartmentId()) {
                    chartVo.setDepartmentName(department.getDepartmentName());
                }
            }
        }
        return PageVO.of(total,current,chartVos);
    }

    //yuhangyu
    public Result insertCharty(ChartDTOy chartDTO){
        QueryWrapper<Doctor> qwdoctor=new QueryWrapper<>();
        QueryWrapper<Patient> qwpatient=new QueryWrapper<>();
        Chart chart=new Chart();
        System.out.println(chartDTO);
        chart.setChartRecord(chartDTO.getChartRecord());


        chart.setDoctorId(chartDTO.getDoctorId());
        chart.setPatientId(chartDTO.getPatientId());

        chart.setBookingId(chartDTO.getBookingId());
        chart.setDoctorName(chartDTO.getDoctorName());
        chart.setPatientName(chartDTO.getPatientName());
        //判断是否有该医生
        qwdoctor.eq("doctor_id",chartDTO.getDoctorId());
        Doctor doctor=doctorMapper.selectOne(qwdoctor);
        if(doctor==null){
            try {
                throw new CustomException("没有该医生");
            } catch (CustomException e) {
                return Result.error(e.getMessage());
            }
        }
        chart.setDepartmentId(doctor.getDepartmentId());
        //是否有该病人
        qwpatient.eq("patient_id",chartDTO.getPatientId());
        Patient patient=patientMapper.selectOne(qwpatient);
        if(patient==null){
            try {
                throw new CustomException("没有该病人的信息");
            } catch (CustomException e) {
                return Result.error(e.getMessage());
            }
        }
        System.out.println("+=+"+chart.toString());
        //查看是否已有该病例
        QueryWrapper<Chart> queryWrapperBookingId=new QueryWrapper<>();
        queryWrapperBookingId.eq("booking_id",chart.getBookingId());
        Chart chartBookingId=chartMapper.selectOne(queryWrapperBookingId);
        if(chartBookingId==null){
            //添加病例

            chartMapper.insert(chart);
            return Result.success("病例添加成功");
        }else{
            chartMapper.update(chart,queryWrapperBookingId);
            return Result.success("更改病例成功");
        }

    }


}
